import { getLocation, getDeviceInfo, getDeviceList, getTrace, closeLiveVideo } from '@/services/device';

export default {
    namespace: 'device',

    state: {
        deviceInfo: {},
        deviceList: [],
        location: [],
        trace: []
    },

    effects: {
        *fetchDeviceList({ payload, callback }, { call, put }) {
            const response = yield call(getDeviceList, payload);
            yield put({
                type: 'deviceList',
                payload: response,
            });
            if (response && callback) {
                callback(response);
            }
        },
        *fetchDeviceInfo({ payload, callback }, { call, put }) {
            const response = yield call(getDeviceInfo, payload);
            yield put({
                type: 'deviceInfo',
                payload: response,
            });
            if (response && callback) {
                callback(response);
            }
        },
        *fetchLocation({ payload, callback }, { call, put }) {
            const response = yield call(getLocation, payload);
            yield put({
                type: 'location',
                payload: response,
            });
            if (response && callback) {
                callback(response);
            }
        },

        *fetchTrace({ payload, callback }, { call, put }) {
            const response = yield call(getTrace, payload);
            yield put({
                type: 'traceList',
                payload: response,
            });
            if (response && callback) {
                callback(response);
            }
        },
        *closeLiveVideo({ payload, callback }, { call, _ }) {
            const response = yield call(closeLiveVideo, payload);
            if (response && callback) {
                callback(response);
            }
        },
    },

    reducers: {
        deviceInfo(state, action) {
            return {
                ...state,
                deviceInfo: action.payload.data
            };
        },
        location(state, action) {
            return {
                ...state,
                location: action.payload.data
            };
        },
        deviceList(state, action) {
            return {
                ...state,
                deviceList: action.payload.data
            };
        },
        traceList(state, action) {
            return {
                ...state,
                trace: action.payload.data
            };
        },
    },
};
